package cn.sppxpp.github.controller;

import cn.sppxpp.github.util.StringConstants;
import lombok.Data;
import org.apache.tomcat.util.http.parser.Authorization;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author spp
 * @date 2020-10-31 20:16
 **/
@Controller
@Data
public class IndexController {
    @Value("${github.clientId}")
    private String clientId;
    @Value("${github.clientSecret}")
    private String clientSecret;
    @Value("${github.redirectUrl}")
    private String redirectUrl;

    @GetMapping("/githubAuth")
    @ResponseBody
    public String login(@RequestParam String code) throws MalformedURLException {
        String accessUrl = StringConstants.GITHUB_ACCESS_TOKEN_URL
                .replace("{authorizeCode}", code)
                .replace("{clientId}", clientId)
                .replace("{clientSecret}", clientSecret);
        System.out.println(accessUrl);
        URL url = new URL(accessUrl);
        try {
            HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            InputStream inData = urlConnection.getInputStream();
            StringBuilder sb = new StringBuilder();
            int len;
            byte[] b = new byte[2048];
            while ((len = inData.read(b)) != -1){
                sb.append(new String(b,0,len));
            }
            System.out.println(urlConnection.getResponseCode());
            inData.close();
            System.out.println(sb);
            String accessToken = sb.substring(sb.indexOf("=") + 1, sb.indexOf("&"));
            System.out.println(accessToken);
            url = new URL(StringConstants.GITHUB_USER_URL);
            HttpsURLConnection userInfoPage = (HttpsURLConnection) url.openConnection();
            userInfoPage.setDoInput(true);
            userInfoPage.setDoOutput(true);
            userInfoPage.setRequestProperty("Authorization","token "+accessToken);
            userInfoPage.setRequestProperty("Content-Type","application/json");
            userInfoPage.setRequestProperty("Accept","application/json");
            InputStream inUserData = userInfoPage.getInputStream();
            sb = new StringBuilder();
            while ((len = inUserData.read(b)) != -1){
                sb.append(new String(b,0,len));
            }
            System.out.println(userInfoPage.getResponseCode());
            inUserData.close();
            System.out.println(sb);
        }catch (IOException io){
            System.out.println(io.getMessage());
        }
        return code;
    }

}
